<template>
    <div>
        <van-cell-group align="left" style="margin-top: 1rem;margin-bottom: 2rem">
            <van-field v-model="myUserId" label="我的ID" placeholder="请输入用户ID"/>
        </van-cell-group>
        <button @click="initWebSocket(myUserId)">连接websocket</button>
        <van-cell-group align="left" style="margin-top: 1rem;margin-bottom: 2rem">
            <van-field v-model="userId" label="用户ID" placeholder="请输入用户ID"/>
            <van-field
                    v-model="message"
                    rows="50"
                    autosize
                    label="留言"
                    type="textarea"
                    maxlength="5000000"
                    placeholder="请输入留言"
                    show-word-limit
            />
        </van-cell-group>


        <van-panel title="接收消息" use-footer-slot>
            <div id="message">

            </div>
        </van-panel>

        <button @click="sendMessage(message)">发送</button>
        <button @click="wxBindpc(message)">绑定</button>
    </div>
</template>

<script>
    import {getComm} from '../api/manage.js';
    import {websocket} from '../mixins/Websocket.js';
    import {Dialog} from 'vant';

    export default {
        name: "webTest",
        mixins:[websocket],
        data() {
            return {
                ws: null,
                userId: '',
                message: '',
                timeout: 570000,
                timeoutObj: null,
                serverTimeoutObj: null,
                myUserId: '10011',
                type: 'lis'
            }
        },
        destroyed() {
            this.ws.close() //离开路由之后断开websocket连接
        },
        methods: {

            wxBindpc() {
                const params = {
                    openid: 'oQtMMwkcwDP1Ex27WxOKcu933lbM',
                    bindType: 'his'
                }

                getComm('/auth/deleteWxPcUser', params).then(res => {
                    console.log(res)
                    alert("成功")
                    if (res.code === 200) {
                        Dialog.confirm({
                            title: '标题',
                            message: '绑定成功可以扫码'
                        })
                    }
                }).catch(err => {
                    console.log("后台请求连接失败" + err)
                })
            },
            sendMessage(data){
                const socketMsg = {toUser: this.userId, type: 99, message: data};
                this.websocketSend(socketMsg)
            }

        },
    }
</script>

<style scoped>

</style>